package services;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.aton.db.SessionFactory;

import models.LevelModelPrice;
import models.LevelModelPrice;
import models.mappers.LevelModelPriceMapper;
import models.mappers.LevelModelPriceMapper;
import vos.LevelModelPriceSearchVo;
import vos.LevelModelPriceVo;
import vos.LevelModelPriceSearchVo;

/**
 * 返款设置操作类
 * 
 * @ClassName: LevelModelPriceService 
 * @author: wangsm
 * @date: 2016年5月31日 上午10:18:57
 */
public class LevelModelPriceService {
	/**
	 * 统计返款设置
	 *
	 * @Title: countLevelModelPrice 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static long countLevelModelPrice(LevelModelPriceSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			LevelModelPriceMapper mapper = ss.getMapper(LevelModelPriceMapper.class);
            return mapper.countLevelModelPrice();
        } finally {
            ss.close();
        }
	}
	/**
	 * 获取返款设置列表
	 *
	 * @Title: listLevelModelPrice 
	 * @param vo
	 * @return
	 * @return: List<RegionVo>
	 */
	public static List<LevelModelPrice> listLevelModelPrice(LevelModelPriceSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			LevelModelPriceMapper mapper = ss.getMapper(LevelModelPriceMapper.class);
            return mapper.listLevelModelPrice(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 批量删除
	 *
	 * @Title: betchDel 
	 * @param levels
	 * @return
	 * @return: boolean
	 */
	public static boolean betchDel(List<Map<String, String>> levels) {
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			LevelModelPriceMapper mapper = ss.getMapper(LevelModelPriceMapper.class);
			for(Map<String, String> level :levels){
				 mapper.delLevelModelPrice(Long.parseLong(level.get("levelid")),level.get("modelid"));
			}
            ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 删除返款设置
	 *
	 * @Title: delLevelModelPrice 
	 * @param id 返款设置id
	 * @return
	 * @return: boolean
	 */
	public static boolean delLevelModelPrice(Long id,String modelid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			LevelModelPriceMapper mapper = ss.getMapper(LevelModelPriceMapper.class);
	        mapper.delLevelModelPrice(id,modelid);
	        ss.commit();
		}
		catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		
		return true;
	}
	/**
	 * 根据id获取返款设置信息
	 *
	 * @Title: getById 
	 * @param levelid 返款设置id
	 * @param modelid 
	 * @return
	 * @return: LevelModelPrice
	 */
	public static LevelModelPriceVo getById(Long levelid, String modelid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			LevelModelPriceMapper mapper = ss.getMapper(LevelModelPriceMapper.class);
			return mapper.getById(levelid,modelid);
		}finally{
			ss.close();
		}
	}
	/**
	 * 保存返款设置信息
	 *
	 * @Title: saveLevelModelPrice 
	 * @param levelmodelprice 返款设置对象
	 * @return
	 * @return: boolean
	 */
	public static boolean saveLevelModelPrice(LevelModelPrice levelmodelprice) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			LevelModelPriceMapper mapper = ss.getMapper(LevelModelPriceMapper.class);
			Date date=new Date();
			levelmodelprice.createtime=date;
	        mapper.saveLevelModelPrice(levelmodelprice);
	        ss.commit();
		}
		catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 更新返款设置信息
	 *
	 * @Title: updateLevelModelPrice 
	 * @param levelmodelprice
	 * @return
	 * @return: boolean
	 */
	public static boolean updateLevelModelPrice(LevelModelPrice levelmodelprice) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			LevelModelPriceMapper mapper = ss.getMapper(LevelModelPriceMapper.class);
			Date date=new Date();
			levelmodelprice.modifytime=date;
	        mapper.updateLevelModelPrice(levelmodelprice);
	        ss.commit();
		}
		catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 导出excel
	 *
	 * @Title: exportXls 
	 * @param vo
	 * @return
	 * @return: List<LevelModelPrice>
	 */
	public static List<LevelModelPriceVo> exportXls(LevelModelPriceSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			LevelModelPriceMapper mapper = ss.getMapper(LevelModelPriceMapper.class);
            return mapper.exportXls(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据城市等级统计设置
	 *
	 * @Title: getByLevel 
	 * @param id
	 * @return
	 * @return: long
	 */
	public static LevelModelPrice getByLevel(String id) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			LevelModelPriceMapper mapper = ss.getMapper(LevelModelPriceMapper.class);
            return mapper.getByLevel(id);
        } finally {
            ss.close();
        }
	}
}
